package com.zjsd.sqeybusiness.service.impl;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.zjsd.sqeybusiness.domain.*;
import com.zjsd.sqeybusiness.mapper.*;
import com.zjsd.sqeybusiness.service.PayBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.service.IOrderinfoService;

/**
 * 接单信息Service业务层处理
 * 
 * @author Zheng
 * @date 2025-03-10
 */
@Service
public class OrderinfoServiceImpl implements IOrderinfoService 
{
    @Autowired
    private OrderinfoMapper orderinfoMapper;
    @Autowired
    private SjinfoMapper sjinfoMapper;
    @Autowired
    private HyinfoMapper hyinfoMapper;
    @Autowired
    private PayBaseService payBaseService;
    @Autowired
    private RechargeinfoMapper rechargeinfoMapper;
    @Autowired
    private AccountlocusMapper accountlocusMapper;
    @Autowired
    private PayorderinfoMapper payorderinfoMapper;

    /**
     * 查询接单信息
     * 
     * @param orderid 接单信息ID
     * @return 接单信息
     */
    @Override
    public Orderinfo selectOrderinfoById(Long orderid)
    {
        return orderinfoMapper.selectOrderinfoById(orderid);
    }

    /**
     * 查询接单信息列表
     * 
     * @param orderinfo 接单信息
     * @return 接单信息
     */
    @Override
    public List<Orderinfo> selectOrderinfoList(Orderinfo orderinfo)
    {
        if(orderinfo.getEndDate() != null){
            Date endDate = orderinfo.getEndDate() ;
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endDate);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            orderinfo.setEndDate(calendar.getTime());
        }
        List<Orderinfo> orderinfoList = orderinfoMapper.selectOrderinfoList(orderinfo);
        for (Orderinfo orderinfo1 : orderinfoList) {
            if(orderinfo1.getSjid()!=null){
                Sjinfo sjinfo = sjinfoMapper.selectSjinfoById(orderinfo1.getSjid());
                if(sjinfo != null){
                    orderinfo1.setSjtitle(sjinfo.getTitle());
                    orderinfo1.setSjcontent(sjinfo.getContent());
                    orderinfo1.setCgrname(sjinfo.getCgrname());
                    if(StringUtils.isBlank(orderinfo.getAdminflag())){
                        if(StringUtils.isNotEmpty(orderinfo1.getZfzt()) && "1".equals(orderinfo1.getZfzt())){
                            orderinfo1.setCgrphone(sjinfo.getCgrphone());
                        }else {
                            //手机号后八位用星号代替
                            String cgrphone = sjinfo.getCgrphone().replaceAll("(\\d{3})\\d{8}(\\d{0})", "$1********$2");
                            orderinfo1.setCgrphone(cgrphone);
                        }
                    }else {
                        orderinfo1.setCgrphone(sjinfo.getCgrphone());
                    }
                    orderinfo1.setProvincename(sjinfo.getProvincename());
                    orderinfo1.setCityname(sjinfo.getCityname());
                    orderinfo1.setAreaname(sjinfo.getAreaname());
                    orderinfo1.setZdsl(sjinfo.getZdsl());
                }
            }
        }
        return orderinfoList;
    }

    /**
     * 新增接单信息
     * 
     * @param orderinfo 接单信息
     * @return 结果
     */
    @Override
    public int insertOrderinfo(Orderinfo orderinfo)
    {
       return orderinfoMapper.insertOrderinfo(orderinfo);
    }

    /**
     * 修改接单信息
     * 
     * @param orderinfo 接单信息
     * @return 结果
     */
    @Override
    public int updateOrderinfo(Orderinfo orderinfo)
    {
        return orderinfoMapper.updateOrderinfo(orderinfo);
    }

    /**
     * 批量删除接单信息
     * 
     * @param orderids 需要删除的接单信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderinfoByIds(Long[] orderids)
    {
        return orderinfoMapper.deleteOrderinfoByIds(orderids);
    }

    /**
     * 删除接单信息信息
     * 
     * @param orderid 接单信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderinfoById(Long orderid)
    {
        return orderinfoMapper.deleteOrderinfoById(orderid);
    }

    @Override
    public String saveOrderinfo(Orderinfo orderinfo) {
        Rechargeinfo rechargeinfo = new Rechargeinfo();
        Hyinfo hyinfo = hyinfoMapper.selectHyinfoById(orderinfo.getHyid());
        Sjinfo sjinfo = sjinfoMapper.selectSjinfoById(orderinfo.getSjid());
        if(hyinfo != null){
            orderinfo.setHyname(hyinfo.getName());
            orderinfo.setHyphone(hyinfo.getPhone());
            orderinfo.setOpenid(hyinfo.getOpenid());
            orderinfo.setWxname(hyinfo.getWxname());
            rechargeinfo.setHyid(hyinfo.getHyid());
            rechargeinfo.setHyname(hyinfo.getName());
            rechargeinfo.setHyphone(hyinfo.getPhone());
            rechargeinfo.setOpenid(hyinfo.getOpenid());
            rechargeinfo.setWxname(hyinfo.getWxname());
            rechargeinfo.setXmmc("购买商机充值");
        }
        rechargeinfo.setCzje(orderinfo.getCzje());
        rechargeinfo.setSjid(orderinfo.getSjid());
        rechargeinfo.setCzsj(new Date());
        rechargeinfoMapper.insertRechargeinfo(rechargeinfo);

        //申诉状态
        orderinfo.setSszt("0");
        orderinfo.setJdsj(new Date());
        orderinfo.setZfzt("0");
        orderinfo.setRechargeid(rechargeinfo.getRechargeid());
        int i =  orderinfoMapper.insertOrderinfo(orderinfo);
        String contenturl  = this.saveAccountlocus(hyinfo,sjinfo,orderinfo);
        return contenturl ;
    }

    @Override
    public Payorderinfo selectOrderinfoByReqsn(Payorderinfo payorderinfo) {
        Payorderinfo payorderinfo1 = payorderinfoMapper.selectPayorderinfoByReqsn(payorderinfo.getReqsn());
       if(payorderinfo1 != null){
           if(StringUtils.isNotEmpty(payorderinfo1.getYwddidlx())){
               //会员购买商机充值
               if("1".equals(payorderinfo1.getYwddidlx())){
                    Orderinfo orderinfo = orderinfoMapper.selectOrderinfoById(payorderinfo1.getOrderid());
                    if(orderinfo != null){
                        payorderinfo1.setZfzt(orderinfo.getZfzt());
                        payorderinfo1.setXmmc("购买商机充值");
                        payorderinfo1.setSjid(orderinfo.getSjid());
                    }
               }
               //会员充值
               if("2".equals(payorderinfo1.getYwddidlx())){
                    Rechargeinfo rechargeinfo = rechargeinfoMapper.selectRechargeinfoById(payorderinfo1.getOrderid());
                    if(rechargeinfo != null){
                        payorderinfo1.setZfzt(rechargeinfo.getZfzt());
                        payorderinfo1.setXmmc("会员年费充值");
                    }
               }
               //升级会员充值
               if("3".equals(payorderinfo1.getYwddidlx())){
                   Rechargeinfo rechargeinfo = rechargeinfoMapper.selectRechargeinfoById(payorderinfo1.getOrderid());
                   if(rechargeinfo != null){
                       payorderinfo1.setZfzt(rechargeinfo.getZfzt());
                       payorderinfo1.setXmmc("会员充值");
                   }
               }
           }
       }
       return payorderinfo1;
    }

    @Override
    public Orderinfo addOrderinfo(Orderinfo orderinfo) {
        Hyinfo hyinfo = hyinfoMapper.selectHyinfoById(orderinfo.getHyid());
        if(hyinfo != null){
            orderinfo.setHyname(hyinfo.getName());
            orderinfo.setHyphone(hyinfo.getPhone());
            orderinfo.setOpenid(hyinfo.getOpenid());
            orderinfo.setWxname(hyinfo.getWxname());
            BigDecimal zhye = hyinfo.getZhye() != null ? hyinfo.getZhye() : BigDecimal.ZERO;
            if(zhye.compareTo(orderinfo.getPrice()) >= 0){
                zhye = zhye.subtract(orderinfo.getPrice() != null ? orderinfo.getPrice() : BigDecimal.ZERO);
                hyinfo.setZhye(zhye);
                hyinfoMapper.updateHyinfo(hyinfo);
                orderinfo.setZhye(zhye);
            }else{
                throw new CustomException("会员账号余额不足");
            }
        }
        Sjinfo sjinfo = sjinfoMapper.selectSjinfoById(orderinfo.getSjid());
        if(sjinfo != null){
            Long jdsl = sjinfo.getJdsl() != null ? sjinfo.getJdsl() : 0L;
            jdsl = jdsl + orderinfo.getJdsl();
            sjinfo.setJdsl(jdsl);
            sjinfoMapper.updateSjinfo(sjinfo);
        }
        orderinfo.setJdsj(new Date());
        orderinfo.setZfzt("1");
        orderinfo.setSszt("0");
        int i = orderinfoMapper.insertOrderinfo(orderinfo);
        return orderinfo ;
    }

    private String  saveAccountlocus(Hyinfo hyinfo, Sjinfo sjinfo , Orderinfo orderinfo) {
        String ip = "";
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            ip =  inetAddress.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        Payorderinfo payorderinfo = payBaseService.createOrder("1", orderinfo.getOrderid(), orderinfo.getCzje().toString(),hyinfo.getHyid(),"购买商机充值",hyinfo.getOpenid(),ip);

        BigDecimal zhye = hyinfo.getZhye() != null ? hyinfo.getZhye() : BigDecimal.ZERO;
        BigDecimal bdqje = zhye ; // 充值前余额
        zhye = zhye.add(orderinfo.getCzje() != null ? orderinfo.getCzje() : BigDecimal.ZERO);
        BigDecimal bdhje = zhye ;
        if(zhye.compareTo(orderinfo.getPrice()) < 0){
            throw new CustomException("充值金额不足，小于商机价格");
        }
        return payorderinfo.getPayinfo();
    }
}
